/* eslint-disable */
import { createSSRApp } from 'vue'
import App from './App.vue'
import store from './store'
import Http from '@/utils/requset'
import { storage } from '@/utils/utils'
import LoginBar from '@/components/login/bar'
import { TOKEN } from '@/assets/js/config'
export function createApp () {
  let timer = null
  const app = createSSRApp(App)
  app.use(store)
  app.config.globalProperties.$https = Http
  app.config.globalProperties.$imgUrl = 'https://img.qumoyugo.com'
  app.config.globalProperties.$countImgUrl = (url = '') => {
    if (!url) { return '' }
    if (url.indexOf('http') === -1) {
      return 'https://img.qumoyugo.com' + url
    } else {
      return url
    }
  }
  app.component('login-bar', LoginBar)
  // #ifdef MP-WEIXIN
  app.config.globalProperties.$subscribeMessage = (tmplIds, callback) => {
    wx.getSetting({
      withSubscriptions: true,
      success: (res) => {
        if (!res.subscriptionsSetting.mainSwitch) {
          wx.showModal({
            content: '检测到您没打开推送权限，是否去设置打开？',
            confirmText: '确认',
            cancelText: '取消',
            success: (res) => {
              if (res.confirm) {
                wx.openSetting({
                  success: (res) => {
                    console.log(res.authSetting)
                  }
                })
              }
            }
          })
        } else {
          wx.requestSubscribeMessage({
            tmplIds,
            success: (res) => {
              const acceptId = []
              for (const k in res) {
                if (res[k] === 'accept') {
                  acceptId.push(k)
                }
              }
              if (!acceptId.length) {
                return false
              }
              wx.login({
                success: async (res) => {
                  const resOpenid = await Http({
                    // 利用code去换取openid
                    url: '/js-token',
                    type: '',
                    data: {
                      code: res.code,
                      scene: 'mp4'
                    }
                  })
                  if (resOpenid.code === 0) {
                    Http({
                      // 添加订阅
                      url: '/subscribe/add',
                      type: '',
                      method: 'POST',
                      data: {
                        templateId: acceptId,
                        openId: resOpenid.data.openid
                      }
                    }).then((res) => {
                      if (res.code === 0) {
                        // eslint-disable-next-line node/no-callback-literal
                        callback(true)
                      }
                    })
                  }
                },
                fail (res) {
                  console.log(3334, res)
                  // uni.showToast({
                  //   title: '订阅失败',
                  //   icon: 'none'
                  // })
                  // eslint-disable-next-line node/no-callback-literal
                  callback(false)
                }
              })
            },
            fail (res) {
              console.log('订阅失败', res)
              // uni.showToast({
              //   title: '订阅失败',
              //   icon: 'none'
              // })
              // eslint-disable-next-line node/no-callback-literal
              callback(false)
            }
          })
        }
      }
    })
  }
  // #endif
  uni.getSystemInfo({
    success: (res) => {
      const { windowWidth, statusBarHeight, screenHeight, safeArea, windowHeight } = res
      app.config.globalProperties.$windowWidth = windowWidth
      app.config.globalProperties.$statusBarHeight = statusBarHeight
      app.config.globalProperties.$safeAreaInsets = screenHeight - safeArea.bottom
      app.config.globalProperties.$windowHeight = windowHeight
      // console.log(res)
    }
  })
  return {
    app
  }
}
